Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
	''' <summary>
	''' Strongly-typed collection for the Threads class.
	''' </summary>
	<Serializable> _
	Public Partial Class ThreadsCollection 
	Inherits ActiveList(Of Threads, ThreadsCollection)
	    Public Sub New()
		End Sub
	End Class
	''' <summary>
	''' This is an ActiveRecord class which wraps the Threads table.
	''' </summary>
	<Serializable> _
	Public Partial Class Threads 
	Inherits ActiveRecord(Of Threads)
		#Region ".ctors and Default Settings"
		
		Public Sub New()
			SetSQLProps()
			InitSetDefaults()
			MarkNew()
		End Sub
		
		Public Sub New(ByVal useDatabaseDefaults As Boolean)
			SetSQLProps()
			If useDatabaseDefaults = True Then
				ForceDefaults()
			End If
			MarkNew()
		End Sub
		Public Sub New(ByVal keyID As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByKey(keyID)
		End Sub
		Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByParam(columnName,columnValue)
		End Sub
		Private Sub InitSetDefaults()
			SetDefaults()
		End Sub
		
		Protected Shared Sub SetSQLProps()
			GetTableSchema()
		End Sub
		#End Region
		
		#Region "Schema and Query Accessor"
		
		Public Shared ReadOnly Property Schema() As TableSchema.Table
			Get
				If BaseSchema Is Nothing Then
					SetSQLProps()
				End If
				Return BaseSchema
			End Get
		End Property
		Private Shared Sub GetTableSchema()
			If (Not IsSchemaInitialized) Then
				'Schema declaration
				Dim schema As TableSchema.Table = New TableSchema.Table("Threads", TableType.Table, DataService.GetInstance("ClubStarterKitData"))
				schema.Columns = New TableSchema.TableColumnCollection()
				schema.SchemaName = "dbo"
				'columns
				
                
                Dim colvarThreadid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarThreadid.ColumnName = "threadid"
                colvarThreadid.DataType = DbType.Int32
                colvarThreadid.MaxLength = 0
                colvarThreadid.AutoIncrement = true
                colvarThreadid.IsNullable = false
                colvarThreadid.IsPrimaryKey = true
                colvarThreadid.IsForeignKey = false
                colvarThreadid.IsReadOnly = false
                
                
                schema.Columns.Add(colvarThreadid)
                
                Dim colvarLocked As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLocked.ColumnName = "locked"
                colvarLocked.DataType = DbType.Boolean
                colvarLocked.MaxLength = 0
                colvarLocked.AutoIncrement = false
                colvarLocked.IsNullable = false
                colvarLocked.IsPrimaryKey = false
                colvarLocked.IsForeignKey = false
                colvarLocked.IsReadOnly = false
                
						colvarLocked.DefaultSetting = "((0))"
				
                
                schema.Columns.Add(colvarLocked)
                
                Dim colvarLastpostdate As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLastpostdate.ColumnName = "lastpostdate"
                colvarLastpostdate.DataType = DbType.DateTime
                colvarLastpostdate.MaxLength = 0
                colvarLastpostdate.AutoIncrement = false
                colvarLastpostdate.IsNullable = true
                colvarLastpostdate.IsPrimaryKey = false
                colvarLastpostdate.IsForeignKey = false
                colvarLastpostdate.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLastpostdate)
                
                Dim colvarLastpostby As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLastpostby.ColumnName = "lastpostby"
                colvarLastpostby.DataType = DbType.Guid
                colvarLastpostby.MaxLength = 0
                colvarLastpostby.AutoIncrement = false
                colvarLastpostby.IsNullable = true
                colvarLastpostby.IsPrimaryKey = false
                colvarLastpostby.IsForeignKey = false
                colvarLastpostby.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLastpostby)
                
                Dim colvarTopicid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarTopicid.ColumnName = "topicid"
                colvarTopicid.DataType = DbType.Int32
                colvarTopicid.MaxLength = 0
                colvarTopicid.AutoIncrement = false
                colvarTopicid.IsNullable = false
                colvarTopicid.IsPrimaryKey = false
                colvarTopicid.IsForeignKey = true
                colvarTopicid.IsReadOnly = false
                
                
				colvarTopicid.ForeignKeyTableName = "Topics"
                
                schema.Columns.Add(colvarTopicid)
                
                Dim colvarViews As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarViews.ColumnName = "views"
                colvarViews.DataType = DbType.Int32
                colvarViews.MaxLength = 0
                colvarViews.AutoIncrement = false
                colvarViews.IsNullable = false
                colvarViews.IsPrimaryKey = false
                colvarViews.IsForeignKey = false
                colvarViews.IsReadOnly = false
                
						colvarViews.DefaultSetting = "((0))"
				
                
                schema.Columns.Add(colvarViews)
                
                Dim colvarSticky As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarSticky.ColumnName = "sticky"
                colvarSticky.DataType = DbType.Boolean
                colvarSticky.MaxLength = 0
                colvarSticky.AutoIncrement = false
                colvarSticky.IsNullable = false
                colvarSticky.IsPrimaryKey = false
                colvarSticky.IsForeignKey = false
                colvarSticky.IsReadOnly = false
                
						colvarSticky.DefaultSetting = "((0))"
				
                
                schema.Columns.Add(colvarSticky)
                
                Dim colvarAdmin As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarAdmin.ColumnName = "admin"
                colvarAdmin.DataType = DbType.Boolean
                colvarAdmin.MaxLength = 0
                colvarAdmin.AutoIncrement = false
                colvarAdmin.IsNullable = false
                colvarAdmin.IsPrimaryKey = false
                colvarAdmin.IsForeignKey = false
                colvarAdmin.IsReadOnly = false
                
                
                schema.Columns.Add(colvarAdmin)
                
				BaseSchema = schema
				
				'add this schema to the provider
                'so we can query it later
                DataService.Providers("ClubStarterKitData").AddSchema("Threads",schema)
			End If
		End Sub
		Public Shared Function CreateQuery() As Query
			Return New Query(Schema)
		End Function
		
		#End Region
		
		#Region "Props"
	
          
        <XmlAttribute("Threadid")> _
        Public Property Threadid As Integer 
			Get
				Return GetColumnValue(Of Integer)("threadid")
			End Get
		    
			Set
				SetColumnValue("threadid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Locked")> _
        Public Property Locked As Boolean 
			Get
				Return GetColumnValue(Of Boolean)("locked")
			End Get
		    
			Set
				SetColumnValue("locked", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Lastpostdate")> _
        Public Property Lastpostdate As Nullable(Of DateTime) 
			Get
				Return GetColumnValue(Of Nullable(Of DateTime))("lastpostdate")
			End Get
		    
			Set
				SetColumnValue("lastpostdate", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Lastpostby")> _
        Public Property Lastpostby As Nullable(Of Guid) 
			Get
				Return GetColumnValue(Of Nullable(Of Guid))("lastpostby")
			End Get
		    
			Set
				SetColumnValue("lastpostby", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Topicid")> _
        Public Property Topicid As Integer 
			Get
				Return GetColumnValue(Of Integer)("topicid")
			End Get
		    
			Set
				SetColumnValue("topicid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Views")> _
        Public Property Views As Integer 
			Get
				Return GetColumnValue(Of Integer)("views")
			End Get
		    
			Set
				SetColumnValue("views", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Sticky")> _
        Public Property Sticky As Boolean 
			Get
				Return GetColumnValue(Of Boolean)("sticky")
			End Get
		    
			Set
				SetColumnValue("sticky", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Admin")> _
        Public Property Admin As Boolean 
			Get
				Return GetColumnValue(Of Boolean)("admin")
			End Get
		    
			Set
				SetColumnValue("admin", Value)
			End Set
		End Property
		
		
		
		
		#End Region
		
		
			    
	    
	    #Region "PrimaryKey Methods"
	    
			Public Function MembersThreadsRecords() As Data.MembersThreadsCollection 
	
				Return New Data.MembersThreadsCollection().Where(MembersThreads.Columns.Threadid, Threadid).Load()
	
			End Function
			
			Public Function MessagesRecords() As Data.MessagesCollection 
	
				Return New Data.MessagesCollection().Where(Messages.Columns.Threadid, Threadid).Load()
	
			End Function
			
		#End Region
		
		
		
		
		
		
	    
	    #Region "ForeignKey Methods"
	    
		''' <summary>
		''' Returns a Topics ActiveRecord object related to this Threads
		''' </summary>
		Public Property Topics() As Data.Topics
			Get
				Return Data.Topics.FetchByID(Me.Topicid)
			End Get
			Set
				SetColumnValue("topicid", Value.Topicid)
			End Set
		End Property
	    
	    #End Region
	    
		
	    
	    #Region "Many To Many Helpers"
	    
	     
        Public Function GetMemberInfoCollection() As Data.MemberInfoCollection 
	        Return Threads.GetMemberInfoCollection(Me.Threadid)
		End Function
		
        Public Shared Function GetMemberInfoCollection(ByVal varThreadid As Integer) As Data.MemberInfoCollection
        
            Dim cmd As SubSonic.QueryCommand = New SubSonic.QueryCommand("SELECT * FROM MemberInfo INNER JOIN MembersThreads ON MemberInfo.memberid=MembersThreads.memberid WHERE MembersThreads.threadid=@threadid", Threads.Schema.Provider.Name)
            
            cmd.AddParameter("@threadid", varThreadid, DbType.Int32)
            Dim rdr As IDataReader = SubSonic.DataService.GetReader(cmd)
            Dim coll As MemberInfoCollection = New MemberInfoCollection()
            coll.LoadAndCloseReader(rdr)
            Return coll
            
        End Function
        
        Public Shared Sub SaveMemberInfoMap(ByVal varThreadid As Integer, ByVal items As MemberInfoCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE threadid=@threadid", Threads.Schema.Provider.Name)
            cmdDel.AddParameter("@threadid", varThreadid)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            For Each item As MemberInfo In items 
				Dim varMembersThreads As MembersThreads = New MembersThreads()
				varMembersThreads.SetColumnValue("threadid", varThreadid)
				varMembersThreads.SetColumnValue("memberid", item.GetPrimaryKeyValue())
				varMembersThreads.Save()
			Next
			
        End Sub
        
        
        Public Shared Sub SaveMemberInfoMap(ByVal varThreadid As Integer, ByVal itemList As System.Web.UI.WebControls.ListItemCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE threadid=@threadid", Threads.Schema.Provider.Name)
            cmdDel.AddParameter("@threadid", varThreadid)
            'add this in
            coll.Add(cmdDel)
            DataService.ExecuteTransaction(coll)
            For Each l As System.Web.UI.WebControls.ListItem In itemList 
            
                If l.Selected
				    
					Dim varMembersThreads As MembersThreads = New MembersThreads()
					varMembersThreads.SetColumnValue("threadid", varThreadid)
					varMembersThreads.SetColumnValue("memberid", l.Value)
					varMembersThreads.Save()
				    
                End If
            Next
        End Sub
        
        Public Shared Sub SaveMemberInfoMap(ByVal varThreadid As Integer, ByVal itemList() As Integer) 
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE threadid=@threadid", Threads.Schema.Provider.Name)
            cmdDel.AddParameter("@threadid", varThreadid)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            
            For Each item As Integer In itemList
            
                	Dim varMembersThreads As MembersThreads = New MembersThreads()
					varMembersThreads.SetColumnValue("threadid", varThreadid)
					varMembersThreads.SetColumnValue("memberid", item)
					varMembersThreads.Save()
            Next
            
        End Sub
        
		Public Shared Sub DeleteMemberInfoMap(ByVal varThreadid As Integer) 
		
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM MembersThreads WHERE threadid=@threadid", Threads.Schema.Provider.Name)
            cmdDel.AddParameter("@threadid", varThreadid)
            DataService.ExecuteQuery(cmdDel)
        End Sub
	      
	      
	      
	    
	    #End Region
	    
		
		#Region "ObjectDataSource support"
		
		''' <summary>
		''' Inserts a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Insert(ByVal varLocked As Boolean,ByVal varLastpostdate As Nullable(Of DateTime),ByVal varLastpostby As Nullable(Of Guid),ByVal varTopicid As Integer,ByVal varViews As Integer,ByVal varSticky As Boolean,ByVal varAdmin As Boolean)
			Dim item As Threads = New Threads()
			
            item.Locked = varLocked
            
            item.Lastpostdate = varLastpostdate
            
            item.Lastpostby = varLastpostby
            
            item.Topicid = varTopicid
            
            item.Views = varViews
            
            item.Sticky = varSticky
            
            item.Admin = varAdmin
            
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		''' <summary>
		''' Updates a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Update(ByVal varThreadid As Integer,ByVal varLocked As Boolean,ByVal varLastpostdate As Nullable(Of DateTime),ByVal varLastpostby As Nullable(Of Guid),ByVal varTopicid As Integer,ByVal varViews As Integer,ByVal varSticky As Boolean,ByVal varAdmin As Boolean)
			Dim item As Threads = New Threads()
		    
                item.Threadid = varThreadid
				
                item.Locked = varLocked
				
                item.Lastpostdate = varLastpostdate
				
                item.Lastpostby = varLastpostby
				
                item.Topicid = varTopicid
				
                item.Views = varViews
				
                item.Sticky = varSticky
				
                item.Admin = varAdmin
				
			item.IsNew = False
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		#End Region
		#Region "Columns Struct"
		Public Structure Columns
			Dim x as Integer
			
            Public Shared Threadid As String = "threadid"
            
            Public Shared Locked As String = "locked"
            
            Public Shared Lastpostdate As String = "lastpostdate"
            
            Public Shared Lastpostby As String = "lastpostby"
            
            Public Shared Topicid As String = "topicid"
            
            Public Shared Views As String = "views"
            
            Public Shared Sticky As String = "sticky"
            
            Public Shared Admin As String = "admin"
            
		End Structure
		#End Region
	End Class
End Namespace
